﻿using Albatross.Shell.Configuration;
using Albatross.WPF.Common.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Albatross.Shell.ViewModels {
	public class ModuleViewModel : WorkspaceViewModel{
		public const string DefaultModuleIconPath = "/Albatross;component/Images/Module_32.png";

		public ModuleViewModel(ModuleCfgElement cfgElement) {
			CfgElement = cfgElement;
			Update();
		}

		public ModuleCfgElement CfgElement { get; private set; }

		public const string NamePropertyName = "Name";
		private string _name;
		public string Name {
			get { return _name; }
			set {
				if (_name == value) { return; }
				RaisePropertyChanging(NamePropertyName);
				_name = value;
				RaisePropertyChanged(NamePropertyName);
			}
		}
		public const string ClassNamePropertyName = "ClassName";
		private string _className;
		public string ClassName {
			get { return _className; }
			set {
				if (_className == value) { return; }
				RaisePropertyChanging(ClassNamePropertyName);
				_className = value;
				RaisePropertyChanged(ClassNamePropertyName);
			}
		}
		public const string IsEnabledPropertyName = "IsEnabled";
		private bool _isEnabled;
		public bool IsEnabled {
			get { return _isEnabled; }
			set {
				if (_isEnabled == value) { return; }
				RaisePropertyChanging(IsEnabledPropertyName);
				_isEnabled = value;
				RaisePropertyChanged(IsEnabledPropertyName);
			}
		}
		public const string AssemblyPropertyName = "Assembly";
		private string _assembly;
		public string Assembly {
			get { return _assembly; }
			set {
				if (_assembly == value) { return; }
				RaisePropertyChanging(AssemblyPropertyName);
				_assembly = value;
				RaisePropertyChanged(AssemblyPropertyName);
			}
		}
		public const string AssemblyVersionPropertyName = "AssemblyVersion";
		private string _assemblyVersion;
		public string AssemblyVersion {
			get { return _assemblyVersion; }
			set {
				if (_assemblyVersion == value) { return; }
				RaisePropertyChanging(AssemblyVersionPropertyName);
				_assemblyVersion = value;
				RaisePropertyChanged(AssemblyVersionPropertyName);
			}
		}
		public const string FileVersionPropertyName = "FileVersion";
		private string _fileVersion;
		public string FileVersion {
			get { return _fileVersion; }
			set {
				if (_fileVersion == value) { return; }
				RaisePropertyChanging(FileVersionPropertyName);
				_fileVersion = value;
				RaisePropertyChanged(FileVersionPropertyName);
			}
		}
		public const string StatusPropertyName = "Status";
		private string _status;
		public string Status {
			get { return _status; }
			set {
				if (_status == value) { return; }
				RaisePropertyChanging(StatusPropertyName);
				_status = value;
				RaisePropertyChanged(StatusPropertyName);
			}
		}
		public const string IconPropertyName = "Icon";
		private string _icon;
		public string Icon {
			get { return _icon; }
			set {
				if (_icon == value) { return; }
				RaisePropertyChanging(IconPropertyName);
				_icon = value;
				RaisePropertyChanged(IconPropertyName);
			}
		}
		public void Update() {
			Name = CfgElement.Name;
			ClassName = CfgElement.ClassName;
			IsEnabled = CfgElement.IsEnabled;
			if (string.IsNullOrEmpty(CfgElement.Icon)) {
				Icon = DefaultModuleIconPath;
			} else {
				Icon = CfgElement.Icon;
			}
			Type type = Type.GetType(ClassName);
			if (type == null) {
				Assembly = null;
				AssemblyVersion = null;
				FileVersion = null;
				Status = "Missing";
			} else {
				Assembly = type.Assembly.FullName;
				Version version = type.Assembly.GetName().Version;
				AssemblyVersion = string.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision);
				FileVersion = FileVersionInfo.GetVersionInfo(type.Assembly.Location).FileVersion;
				Status = "Online";
			}
		}
	}
}
